Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!

Paste

Pasted as C by ciccio ( 4 years ago )
#include <avr/io.h>
#include <stdbool.h>
#include <util/delay.h>
#include <avr/interrupt.h> 

/* Define Global variables */
volatile uint8_t key_matrix[8];
const static uint8_t table[8]={0b01000000, 0b01100000, 0b01010000, 0b01110000, 0b01001000, 0b01101000, 0b01011000, 0b01111000};
uint8_t CID=2;

/* Define Econder Pins */
#define ROTA !((1<<PC2)&PINC)
#define ROTB !((1<<PC3)&PINC)

extern void inline switches_init(void)
{
	/* Setup the ports */
	DDRA  = 0xFF;	// All pins are OUTPUT
        PORTA = 0x00;
	DDRB  = 0xFF;	// All pins are OUTPUT
        PORTB = 0x00;

        DDRC  = 0x00;   // All pins are INPUT
	PORTC = 0xFF;	// Enable pull ups

        DDRD  = 0xFF;   // All pins are OUTPUT
}

//Initialize the key_matrix
extern void init_key_matrix(void)
{
    uint8_t i;
    for (i = 0; i < 8; i++)
    {
        key_matrix[i] = 0x00;
    }
}

//Set one flag based on the address
inline void set_key_matrix(uint8_t address)
{
    key_matrix[address/8] |= 0x01 << (address % 8);
}

//Clear one flag based on the address
inline void clear_key_matrix(uint8_t address)
{
    key_matrix[address/8] &= ~(0x01 << (address % 8));
}

//Check whether a flag is set
inline bool get_key_matrix_state(uint8_t address)
{
    return (key_matrix[address/8] & (0x1 << (address % 0x8))) != 0x00;
}


//Cycle on the buttons
void switches_tick()
{

 uint8_t i, j, value, key_pressed, key_index;
 uint8_t wait=0;
 bool pressed;

 for(i=0; i<=7; i++) {

   //PORTD |=  (i & 0x01) << 5 | ((i >> 1) & 0x01) << 4 | ((i >> 2) & 0x01) << 3;
  
   PORTD = table[i]; 
    _delay_us(1);   
   for (j=0; j<=7; j++) {

    //Define some key variables
    value = ( 1<<j );
    key_pressed = PINC & value;
    key_index   = 8*i+j;
    pressed     = get_key_matrix_state(key_index);
 
    //Encoder section
    if (key_index==2 || key_index==3) {
     if(ROTA & (!wait))
       wait=1;
     if (ROTB & ROTA & (wait))
      {
        green_led_blink();
        wait=2;
        fsbus_snd(CID, key_index, -1, 3);
      }
     else if(ROTA & (!ROTB) & wait)
      {
        green_led_blink();
        wait=2;
        fsbus_snd(CID, key_index, 1, 3);
      }
     if ((!ROTA)&!(ROTB)&(wait==2))
     wait=0;
    }
    else {
    //Check if the key is low or high and then send the message
     if ( !key_pressed && !pressed ) {
      fsbus_snd(CID, key_index, 1, 3);
      green_led_blink();
      set_key_matrix(key_index);
     }
     else if ( key_pressed && pressed) {
      fsbus_snd(CID, key_index, 0, 3);
      green_led_blink();
      clear_key_matrix(key_index);
     }
    }
   }
  }
}

 

Revise this Paste

Your Name: Code Language: